import Vue from "vue";
import VueRouter from "vue-router";
import Bus from "@/js/EventBus";
Vue.use(VueRouter);
// 解决vue路由重复导航错误
//获取原型对象上的push函数
//修改原型对象中的push方法
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};
const routes = [
  {
    path: "/",
    name: "index",
    redirect: "/cesium2d",
    component: () => import("@/views/index/Index.vue"),
    children: [
      {
        path: "/cesium2d",
        name: "cesium2d",
        component: () => import("@/views/map/Cesium2d.vue"),
      },
      {
        path: "/cesium3d",
        name: "cesium3d",
        component: () => import("@/views/map/Cesium3d.vue"),
      },
    ],
  },
  // {
  //   path: '/',
  //   name: 'index',
  //   component: () => import('@/views/map/Cesium3d.vue'),
  // },
];

const router = new VueRouter({
  mode: "history",
  // base: process.env.BASE_URL,
  routes,
});
// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  Bus.$emit("xiaohui", true);
  // to将要访问的路径
  // from 从哪个路径过来
  // next 是一个函数，表示放行
  // next() 放行 next('./login') 强制跳转
  // console.log(to, "to");
  // console.log(from, "from");
  // console.log(next, "next");

  // if (to.path === "/") return next();
  if (from.path == "/" && to.path == "/cesium2d") {
    window.sessionStorage.setItem("path", "/cesium2d");
    window.sessionStorage.setItem("btntype", 2);
  } else if (from.path == "/" && to.path == "/cesium3d") {
    window.sessionStorage.setItem("path", "/cesium3d");
    window.sessionStorage.setItem("btntype", 3);
  }
  // 获取token
  // const btntype = window.sessionStorage.getItem("btntype");
  // console.log(tokenStr);
  // if (!btntype) return next("/cesium2d");
  // if (btntype) {
  //   if (btntype == 2) {
  //     return next("/cesium2d");
  //   } else if (btntype == 3) {
  //     return next("/cesium3d");
  //   }
  // }
  // next("/")
  next();
});

export default router;
